﻿using System;
using System.ComponentModel;
using System.ComponentModel.Design;
using System.Collections;
using System.Drawing;
using System.Linq;
using System.Workflow.ComponentModel;
using System.Workflow.ComponentModel.Design;
using System.Workflow.ComponentModel.Compiler;
using System.Workflow.ComponentModel.Serialization;
using System.Workflow.Runtime;
using System.Workflow.Activities;
using System.Workflow.Activities.Rules;
using System.IO;
using System.Xml;
using BPM.Base.DataTypeLib;
using BPM.Foundation.WFService;

namespace BPM.Base.WFLib
{
    [System.Drawing.ToolboxBitmap(typeof(初始化), "pic.callchildlist.png")]
	public partial class 调用多个子流程: Activity
	{

        public static DependencyProperty 数据表单Property = DependencyProperty.Register("数据表单", typeof(string), typeof(调用多个子流程), new PropertyMetadata(""));

        [DescriptionAttribute("")]
        [CategoryAttribute("")]
        [BrowsableAttribute(true)]
        [DesignerSerializationVisibilityAttribute(DesignerSerializationVisibility.Visible)]
        public string 数据表单
        {
            get
            {
                return ((string)(base.GetValue(调用多个子流程.数据表单Property)));
            }
            set
            {
                base.SetValue(调用多个子流程.数据表单Property, value);
            }
        }



        protected override ActivityExecutionStatus Execute(ActivityExecutionContext executionContext)
        {

            //判断数据表单是否为空
            if (!string.IsNullOrEmpty(this.数据表单))
            {
                StringReader stream = null;
                XmlTextReader reader = null;


                DataForm myDataForm = new DataForm();

                //把数据表单字符串读取到StringReader
                stream = new StringReader(this.数据表单);

                //从stream装载到XmlTextReader

                reader = new XmlTextReader(stream);


                //把xmlTextReader 读到 dataSet
                try
                {
                    myDataForm.ReadXml(reader);
                }
                catch
                {
                    myDataForm = null;
                }

                //判断转化后的dataset是否为空
                if (myDataForm != null)
                {

                    //判断childWorkflowTab是否为空
                    if (myDataForm.childWorkflowTab != null)
                    {

                        WorkflowService service = executionContext.GetService(typeof(WorkflowService)) as WorkflowService;

                        //判断服务是否为空
                        if (service != null)
                        {
                            foreach (DataForm.childWorkflowTabRow childRow in myDataForm.childWorkflowTab.Rows)
                            {


                                #region "启动子流程"

                                //业务信息
                                BusinessParameter bParameter = new BusinessParameter();
                                bParameter.businessDescription = childRow.businessDescription;
                                bParameter.businessID = childRow.businessID;
                                bParameter.businessName = childRow.businessName;
                                bParameter.businessType = childRow.businessType;
                                bParameter.masterBusinessID = childRow.masterBusinessID;
                                bParameter.secrecy = childRow.secrecy;
                                bParameter.startDepartment = childRow.startDepartment;
                                bParameter.startDuty = childRow.startDuty;
                                bParameter.startTime = DateTime.Now;
                                bParameter.startUser = childRow.startUser;
                               

                                if (childRow.testID < 0)
                                {
                                    bParameter.testID = 0;
                                }
                                else
                                {
                                    bParameter.testID = childRow.testID;
                                }


                                bParameter.urgency = childRow.urgency;

                                //子流程信息
                                ChildWorkflowParameter childParameter = new ChildWorkflowParameter();
                                childParameter.masterInstanceID = childRow.masterInstanceID;
                                if (childRow.masterInstanceID == Guid.Empty)
                                {
                                    childParameter.masterInstanceID = this.WorkflowInstanceId;
                                }
                                childParameter.regress = childRow.regress;
                                childParameter.regressGroup = childRow.regressGroup;

                                //权限信息
                                PowerParameter pwParameter = new PowerParameter();
                                pwParameter.manageDepartment = childRow.manageDepartment;
                                pwParameter.manageDuty = childRow.manageDuty;
                                pwParameter.manageUser = childRow.manageUser;
                                pwParameter.overtimeOperate = childRow.overtimeOperate;
                                pwParameter.seeDepartment = childRow.seeDepartment;
                                pwParameter.seeDuty = childRow.seeDuty;
                                pwParameter.seeUser = childRow.seeUser;
                                pwParameter.startUserPower = childRow.startUserPower;
                                pwParameter.transactTimelimit = childRow.transactTimelimit;


                                //流程信息
                                WorkflowStartParameter wfParam = new WorkflowStartParameter();
                                wfParam.businessParameter = bParameter;
                                wfParam.childWorkflowParameter = childParameter;
                                
                                if(childRow.IsinstanceIDNull())
                                {
                                    childRow.instanceID = Guid.Empty;
                                }
                                wfParam.instanceID = childRow.instanceID;

                                if (string.IsNullOrEmpty(childRow.dataForm))
                                {
                                    wfParam.dataForm = null;
                                }
                                else
                                {
                                    //wfParam.dataForm = this.数据表单;
                                }
                                wfParam.nextStateUserNo = childRow.nextStateUserNo;
                                wfParam.powerParameter = pwParameter;
                                wfParam.templateEdition = childRow.templateEdition;
                                wfParam.templateID = childRow.templateID;




                                service.createWorkflow("", wfParam);

                                //延迟2.5秒,防止后续的数据操作出现问题
                                //System.Threading.Thread.Sleep(2500);

                                #endregion


                            }
                        }

                    }
                }
            }
            return base.Execute(executionContext);
        }
	}
}
